package com.hyj.ecp.clientservice.order.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ecp.order.OrderQO;
import com.hyj.ecp.clientservice.order.service.IOrderService;
import com.hyj.ecp.common.constant.SysConstant;
import com.hyj.util.HttpUtil;
import com.hyj.util.JsonUtil;

@Controller
@RequestMapping("/client/order/*")
public class OrderAction {

	Log logger = LogFactory.getLog(OrderAction.class);
	@Autowired
    private IOrderService service;
	
	@RequestMapping(value="saveOrder.do", method=RequestMethod.POST)
    public void saveOrder(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
		logger.info("saveOrder.do start...");
		//String userNum=request.getHeader("userNum");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			OrderQO qo =  JsonUtil.fromJson(reqparam, OrderQO.class);
			qo.setStatus(SysConstant.ORDER_DDFK);
			OrderQO vo = service.saveOrder(qo);
		    if(vo.getId()!=0){
			   map.put("ReturnCode", 0);
			   map.put("msg","提交订单成功！");
			   map.put("OrderInfo", vo);
		    }else{
		    	map.put("ReturnCode", 999);
				map.put("msg","提交订单失败！");
		    }
			 
		}catch(Exception e){
			logger.error("saveOrder.do 出错",e);
			map.put("ReturnCode", 999);
			map.put("msg","提交订单失败！"+e.getMessage());
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("saveOrder.do end");
    	
    }
	
	
	@RequestMapping(value="queryOrderList.do", method=RequestMethod.POST)
    public void queryOrderList(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
		logger.info("queryOrderList.do start...");
		//String userNum=request.getHeader("userNum");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数:" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			OrderQO qo =  JsonUtil.fromJson(reqparam, OrderQO.class);
			
		    List<Map<String,Object>> list = service.queryOrderList(qo);
		    map.put("ReturnCode", 0);
		    map.put("msg","查询订单列表成功！");
		    map.put("List",list);
		   
		}catch(Exception e){
			logger.error("queryOrderList.do 出错",e);
			map.put("ReturnCode", 999);
			map.put("msg","查询订单失败！");
			map.put("List",null);
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("queryOrderList.do end");
    	
    }
	
	
	@RequestMapping(value="queryOrderInfo.do", method=RequestMethod.POST)
    public void queryOrderInfo(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
		logger.info("queryOrderInfo.do start...");
		//String userNum=request.getHeader("userNum");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数:" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			OrderQO qo =  JsonUtil.fromJson(reqparam, OrderQO.class);
			
		    Map<String,Object> vo = service.queryOrderInfo(qo);
		    map.put("ReturnCode", 0);
		    map.put("msg","查询订单成功！");
		    map.put("info",vo);
		   
		}catch(Exception e){
			logger.error("queryOrderInfo.do 出错",e);
			map.put("ReturnCode", 999);
			map.put("msg","查询订单失败！");
			map.put("info",null);
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("queryOrderInfo.do end");
    	
    }
	
	
	/**
	 * 支付宝 回调接口
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="aliSecurity.do", method=RequestMethod.POST)
    public void aliSecurity(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
		logger.info("aliSecurity.do start...");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数:" + reqparam);
		String ret = "";
		try{
			int a = service.updateAliSecurity(reqparam);
			ret ="success";
		}catch(Exception e){
			logger.error("aliSecurity.do 出错",e);
		}
		 
		
		logger.info("返回参数：" + ret);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(ret.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("aliSecurity.do end");
    	
    }
}
